package edu.thu.studio.ormdesigner.editor.contributor;

import java.util.Iterator;

import org.eclipse.gef.ui.actions.ActionBarContributor;
import org.eclipse.gef.ui.actions.DeleteRetargetAction;
import org.eclipse.gef.ui.actions.GEFActionConstants;
import org.eclipse.gef.ui.actions.RedoRetargetAction;
import org.eclipse.gef.ui.actions.UndoRetargetAction;
import org.eclipse.gef.ui.actions.ZoomComboContributionItem;
import org.eclipse.gef.ui.actions.ZoomInRetargetAction;
import org.eclipse.gef.ui.actions.ZoomOutRetargetAction;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.RetargetAction;
import org.eclipse.ui.part.EditorPart;

import edu.thu.studio.ormdesigner.Activator;
import edu.thu.studio.ormdesigner.editor.action.ShortCutAction;
import edu.thu.studio.ormdesigner.editor.action.ZoomAdjustAction;
import edu.thu.studio.ormdesigner.editor.action.ZoomAdjustRetargetAction;
import edu.thu.studio.ormdesigner.util.Properties;

/**
 * 重定向操作分为已定义和未定义 已定义:redo/undo 未定义:需要定义操作
 */
public class ORMModelActionBarContributor extends ActionBarContributor {
    
    @Override
    protected void buildActions() {
        // 删除
        this.addRetargetAction(new DeleteRetargetAction());
        // 撤销
        this.addRetargetAction(new UndoRetargetAction());
        // 反撤销
        this.addRetargetAction(new RedoRetargetAction());
        
        // 缩放
        ZoomInRetargetAction zoomInAction = new ZoomInRetargetAction();
        zoomInAction.setImageDescriptor(Activator.getImageDescriptor(Properties.icon_zoom_in));
        ZoomOutRetargetAction zoomOutAction = new ZoomOutRetargetAction();
        zoomOutAction.setImageDescriptor(Activator.getImageDescriptor(Properties.icon_zoom_out));
        this.addRetargetAction(new ZoomAdjustRetargetAction());
        this.addRetargetAction(zoomInAction);
        this.addRetargetAction(zoomOutAction);
        
        // 复制、粘贴
        RetargetAction copyAction = new RetargetAction(ActionFactory.COPY.getId(), Properties.action_title_copy);
        copyAction.setImageDescriptor(Activator.getImageDescriptor(Properties.icon_copy));
        this.addRetargetAction(copyAction);
        RetargetAction pasteAction = new RetargetAction(ActionFactory.PASTE.getId(), Properties.action_title_paste);
        pasteAction.setImageDescriptor(Activator.getImageDescriptor(Properties.icon_paste));
        this.addRetargetAction(pasteAction);
        
        // 拷贝快照
        RetargetAction shortCutAction = new RetargetAction(ShortCutAction.ID, Properties.editor_ctrlk);
        shortCutAction.setImageDescriptor(Activator.getImageDescriptor(Properties.icon_ctrlk));
        this.addRetargetAction(shortCutAction);
    }
    
    @Override
    protected void declareGlobalActionKeys() {
        
    }
    
    public void contributeToToolBar(IToolBarManager toolBarManager, ZoomComboContributionItem zoomComboContributionItem) {
        toolBarManager.add(getAction(ActionFactory.DELETE.getId()));
        toolBarManager.add(getAction(ActionFactory.UNDO.getId()));
        toolBarManager.add(getAction(ActionFactory.REDO.getId()));
        
        toolBarManager.add(new Separator());
        
        toolBarManager.add(getActionRegistry().getAction(GEFActionConstants.ZOOM_IN));
        toolBarManager.add(getActionRegistry().getAction(GEFActionConstants.ZOOM_OUT));
        toolBarManager.add(getActionRegistry().getAction(ZoomAdjustAction.ID));
        toolBarManager.add(zoomComboContributionItem);
        
        toolBarManager.add(new Separator());

        toolBarManager.add(getAction(ActionFactory.COPY.getId()));
        toolBarManager.add(getAction(ActionFactory.PASTE.getId()));
        toolBarManager.add(getAction(ShortCutAction.ID));
        
    }
    
    @SuppressWarnings("rawtypes")
    public void initRetargetActions(EditorPart newEditor) {
        Iterator iter = this.getActionRegistry().getActions();
        while (iter.hasNext()) {
            IAction action = (IAction) iter.next();
            if (action instanceof RetargetAction) {
                ((RetargetAction) action).partActivated(newEditor);
            }
        }
    }
    
}
